{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "Iter 0, Testing Accuracy= 0.1511, Training Accuracy= 0.158\n",
      "Iter 100, Testing Accuracy= 0.3234, Training Accuracy= 0.3288\n",
      "Iter 200, Testing Accuracy= 0.6009, Training Accuracy= 0.6175\n",
      "Iter 300, Testing Accuracy= 0.6676, Training Accuracy= 0.6708\n",
      "Iter 400, Testing Accuracy= 0.7332, Training Accuracy= 0.7367\n",
      "Iter 500, Testing Accuracy= 0.7568, Training Accuracy= 0.7615\n",
      "Iter 600, Testing Accuracy= 0.9263, Training Accuracy= 0.9242\n",
      "Iter 700, Testing Accuracy= 0.9477, Training Accuracy= 0.9438\n",
      "Iter 800, Testing Accuracy= 0.9544, Training Accuracy= 0.9512\n",
      "Iter 900, Testing Accuracy= 0.9565, Training Accuracy= 0.9532\n",
      "Iter 1000, Testing Accuracy= 0.9629, Training Accuracy= 0.9602\n"
     ]
    }
   ],
   "source": [
    "mnist = input_data.read_data_sets('MNIST_data',one_hot=True)\n",
    "\n",
    "#每个批次的大小\n",
    "batch_size = 100\n",
    "#计算一共有多少个批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "#参数概要\n",
    "def variable_summaries(var):\n",
    "    with tf.name_scope('summaries'):\n",
    "        mean = tf.reduce_mean(var)\n",
    "        tf.summary.scalar('mean', mean)#平均值\n",
    "        with tf.name_scope('stddev'):\n",
    "            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))\n",
    "        tf.summary.scalar('stddev', stddev)#标准差\n",
    "        tf.summary.scalar('max', tf.reduce_max(var))#最大值\n",
    "        tf.summary.scalar('min', tf.reduce_min(var))#最小值\n",
    "        tf.summary.histogram('histogram', var)#直方图\n",
    "\n",
    "#初始化权值\n",
    "def weight_variable(shape,name):\n",
    "    initial = tf.truncated_normal(shape,stddev=0.1)#生成一个截断的正态分布\n",
    "    return tf.Variable(initial,name=name)\n",
    "\n",
    "#初始化偏置\n",
    "def bias_variable(shape,name):\n",
    "    initial = tf.constant(0.1,shape=shape)\n",
    "    return tf.Variable(initial,name=name)\n",
    "\n",
    "#卷积层\n",
    "def conv2d(x,W):\n",
    "    #x input tensor of shape `[batch, in_height, in_width, in_channels]`\n",
    "    #W filter / kernel tensor of shape [filter_height, filter_width, in_channels, out_channels]\n",
    "    #`strides[0] = strides[3] = 1`. strides[1]代表x方向的步长，strides[2]代表y方向的步长\n",
    "    #padding: A `string` from: `\"SAME\", \"VALID\"`\n",
    "    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')\n",
    "\n",
    "#池化层\n",
    "def max_pool_2x2(x):\n",
    "    #ksize [1,x,y,1]\n",
    "    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')\n",
    "\n",
    "#命名空间\n",
    "with tf.name_scope('input'):\n",
    "    #定义两个placeholder\n",
    "    x = tf.placeholder(tf.float32,[None,784],name='x-input')\n",
    "    y = tf.placeholder(tf.float32,[None,10],name='y-input')\n",
    "    with tf.name_scope('x_image'):\n",
    "        #改变x的格式转为4D的向量[batch, in_height, in_width, in_channels]`\n",
    "        x_image = tf.reshape(x,[-1,28,28,1],name='x_image')\n",
    "\n",
    "\n",
    "with tf.name_scope('Conv1'):\n",
    "    #初始化第一个卷积层的权值和偏置\n",
    "    with tf.name_scope('W_conv1'):\n",
    "        W_conv1 = weight_variable([5,5,1,32],name='W_conv1')#5*5的采样窗口，32个卷积核从1个平面抽取特征\n",
    "    with tf.name_scope('b_conv1'):  \n",
    "        b_conv1 = bias_variable([32],name='b_conv1')#每一个卷积核一个偏置值\n",
    "\n",
    "    #把x_image和权值向量进行卷积，再加上偏置值，然后应用于relu激活函数\n",
    "    with tf.name_scope('conv2d_1'):\n",
    "        conv2d_1 = conv2d(x_image,W_conv1) + b_conv1\n",
    "    with tf.name_scope('relu'):\n",
    "        h_conv1 = tf.nn.relu(conv2d_1)\n",
    "    with tf.name_scope('h_pool1'):\n",
    "        h_pool1 = max_pool_2x2(h_conv1)#进行max-pooling\n",
    "\n",
    "with tf.name_scope('Conv2'):\n",
    "    #初始化第二个卷积层的权值和偏置\n",
    "    with tf.name_scope('W_conv2'):\n",
    "        W_conv2 = weight_variable([5,5,32,64],name='W_conv2')#5*5的采样窗口，64个卷积核从32个平面抽取特征\n",
    "    with tf.name_scope('b_conv2'):  \n",
    "        b_conv2 = bias_variable([64],name='b_conv2')#每一个卷积核一个偏置值\n",
    "\n",
    "    #把h_pool1和权值向量进行卷积，再加上偏置值，然后应用于relu激活函数\n",
    "    with tf.name_scope('conv2d_2'):\n",
    "        conv2d_2 = conv2d(h_pool1,W_conv2) + b_conv2\n",
    "    with tf.name_scope('relu'):\n",
    "        h_conv2 = tf.nn.relu(conv2d_2)\n",
    "    with tf.name_scope('h_pool2'):\n",
    "        h_pool2 = max_pool_2x2(h_conv2)#进行max-pooling\n",
    "\n",
    "#28*28的图片第一次卷积后还是28*28，第一次池化后变为14*14\n",
    "#第二次卷积后为14*14，第二次池化后变为了7*7\n",
    "#进过上面操作后得到64张7*7的平面\n",
    "\n",
    "with tf.name_scope('fc1'):\n",
    "    #初始化第一个全连接层的权值\n",
    "    with tf.name_scope('W_fc1'):\n",
    "        W_fc1 = weight_variable([7*7*64,1024],name='W_fc1')#上一场有7*7*64个神经元，全连接层有1024个神经元\n",
    "    with tf.name_scope('b_fc1'):\n",
    "        b_fc1 = bias_variable([1024],name='b_fc1')#1024个节点\n",
    "\n",
    "    #把池化层2的输出扁平化为1维\n",
    "    with tf.name_scope('h_pool2_flat'):\n",
    "        h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64],name='h_pool2_flat')\n",
    "    #求第一个全连接层的输出\n",
    "    with tf.name_scope('wx_plus_b1'):\n",
    "        wx_plus_b1 = tf.matmul(h_pool2_flat,W_fc1) + b_fc1\n",
    "    with tf.name_scope('relu'):\n",
    "        h_fc1 = tf.nn.relu(wx_plus_b1)\n",
    "\n",
    "    #keep_prob用来表示神经元的输出概率\n",
    "    with tf.name_scope('keep_prob'):\n",
    "        keep_prob = tf.placeholder(tf.float32,name='keep_prob')\n",
    "    with tf.name_scope('h_fc1_drop'):\n",
    "        h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob,name='h_fc1_drop')\n",
    "\n",
    "with tf.name_scope('fc2'):\n",
    "    #初始化第二个全连接层\n",
    "    with tf.name_scope('W_fc2'):\n",
    "        W_fc2 = weight_variable([1024,10],name='W_fc2')\n",
    "    with tf.name_scope('b_fc2'):    \n",
    "        b_fc2 = bias_variable([10],name='b_fc2')\n",
    "    with tf.name_scope('wx_plus_b2'):\n",
    "        wx_plus_b2 = tf.matmul(h_fc1_drop,W_fc2) + b_fc2\n",
    "    with tf.name_scope('softmax'):\n",
    "        #计算输出\n",
    "        prediction = tf.nn.softmax(wx_plus_b2)\n",
    "\n",
    "#交叉熵代价函数\n",
    "with tf.name_scope('cross_entropy'):\n",
    "    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction),name='cross_entropy')\n",
    "    tf.summary.scalar('cross_entropy',cross_entropy)\n",
    "    \n",
    "#使用AdamOptimizer进行优化\n",
    "with tf.name_scope('train'):\n",
    "    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\n",
    "\n",
    "#求准确率\n",
    "with tf.name_scope('accuracy'):\n",
    "    with tf.name_scope('correct_prediction'):\n",
    "        #结果存放在一个布尔列表中\n",
    "        correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))#argmax返回一维张量中最大的值所在的位置\n",
    "    with tf.name_scope('accuracy'):\n",
    "        #求准确率\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "        tf.summary.scalar('accuracy',accuracy)\n",
    "        \n",
    "#合并所有的summary\n",
    "merged = tf.summary.merge_all()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    train_writer = tf.summary.FileWriter('logs/train',sess.graph)\n",
    "    test_writer = tf.summary.FileWriter('logs/test',sess.graph)\n",
    "    for i in range(1001):\n",
    "        #训练模型\n",
    "        batch_xs,batch_ys =  mnist.train.next_batch(batch_size)\n",
    "        sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.5})\n",
    "        #记录训练集计算的参数\n",
    "        summary = sess.run(merged,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})\n",
    "        train_writer.add_summary(summary,i)\n",
    "        #记录测试集计算的参数\n",
    "        batch_xs,batch_ys =  mnist.test.next_batch(batch_size)\n",
    "        summary = sess.run(merged,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})\n",
    "        test_writer.add_summary(summary,i)\n",
    "    \n",
    "        if i%100==0:\n",
    "            test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})\n",
    "            train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images[:10000],y:mnist.train.labels[:10000],keep_prob:1.0})\n",
    "            print (\"Iter \" + str(i) + \", Testing Accuracy= \" + str(test_acc) + \", Training Accuracy= \" + str(train_acc))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
